/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ProFormItemProps } from '@alipay/tech-ui';
import { ProForm, FormListContext } from '@alipay/tech-ui';
import type { ComponentProps } from 'react';
import { useContext } from 'react';
import RuleTree from './RuleTree';
import { RuleTreeContext } from './RuleTreeContext';
import { Form } from 'antd';

export type ProFormRuleTreeProps<T> = Omit<ProFormItemProps, 'children'> & {
  fieldProps?: Omit<ComponentProps<typeof RuleTree<T>>, 'children'>
  children?: ComponentProps<typeof RuleTree>['children'] | React.ReactNode
}

/**
 * @deprecated 请使用 RuleTree 组件, 该组件将不再维护, 请尽快迁移，未来的某个版本可能会移除
 */
export default function ProFormRuleTree<T>(props: ProFormRuleTreeProps<T>) {
  const { fieldProps, children, ...restProps } = props;
  const configStore = useContext(RuleTreeContext);

  const _ProForm = configStore.__components?.ProForm || ProForm;

  return (
    <RuleTreeContext.Provider
      value={{
        FormListContext,
        __components: {
          Form,
          ProForm,
        },
        ...configStore,
      }}
    >
      <_ProForm.Item {...restProps}>
        <RuleTree {...fieldProps}>{children as any}</RuleTree>
      </_ProForm.Item>
    </RuleTreeContext.Provider>
  );
}